package com.henu.xyh;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.henu.R;
import com.henu.yizhiya.entity.Person;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class NicknameActivity extends AppCompatActivity {

    private ImageButton nickname_return;
    private EditText nickname_edit;
    private Button nickname_save;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nickname);
        nickname_return= (ImageButton) findViewById(R.id.nickname_return);
        nickname_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                finish();
            }
        });
        nickname_edit= (EditText) findViewById(R.id.nickname_edit);
        final Person person = BmobUser.getCurrentUser(Person.class);
        nickname_edit.setText(person.getNickname());
        nickname_save=(Button) findViewById(R.id.nickname_save);

        nickname_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=nickname_edit.getText().toString().trim();
                person.setNickname(str);
                person.update(person.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e==null){
                            Toast.makeText(NicknameActivity.this, "成功", Toast.LENGTH_SHORT).show();
                            Person p = BmobUser.getCurrentUser(Person.class);
                            Intent intent = new Intent(NicknameActivity.this, MyinformationActivity.class);
                            startActivity(intent);
                            finish();
                        }else{
                            Toast.makeText(NicknameActivity.this, "失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });

    }
}
